import { Message } from 'element-ui'
export default {
  inserted: (el, binding) => {
    el.onclick = () => hand(el, binding)
  },
  update: (el, binding) => {
    el.onclick = () => hand(el, binding)
  },
}

// 事件处理
function hand(el, binding) {
  if (binding.value) copy(binding.value)
  else copy(el.innerText)
}

// 复制到剪贴板
function copy(val) {
  var input = document.createElement('input')
  input.value = val
  document.body.appendChild(input)
  input.select() // 选择对象
  if (document.execCommand('Copy')) {
    Message({
      message: '复制成功',
      type: 'success',
    })
    input.className = 'oCopy'
    input.style.display = 'none'
    setTimeout(() => {
      input.remove()
    }, 1000)
  }
}
